package com.example.demo.Controller;

import com.example.demo.entiy.ReturnModel;
import com.example.demo.entiy.TestList;
import com.example.demo.utils.MyUtils;
import com.example.demo.utils.SqlHelperTest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/sjld")
public class SjldController {
    SqlHelperTest sqlHelperTest=new SqlHelperTest();
    @RequestMapping("/")
    public String index(){
        return "/sjld/index";
    }
    @RequestMapping(value = "/getList1",method = RequestMethod.POST)
    @ResponseBody
    public Object getList1(String id) throws SQLException {
        String status="true";
        String msg="";
        Object obj=null;
        int ids=0;
        if (id.equals("999999")){
            ids=0;
        }else {
            ids=Integer.parseInt(id);
        }
        List<TestList> li=new ArrayList<TestList>();
        Connection con=sqlHelperTest.getConnection();
        String sql="select * from t3 where ssqy="+ids;
        Statement st=null;
        ResultSet rs =null;
        PreparedStatement ps = null;
        try {
            st=con.createStatement();
            rs=st.executeQuery(sql);
            while (rs.next()){
                li.add(new TestList(rs.getString("id"),rs.getString("name").replace(" ",""),rs.getString("ssqy")));
            }
        }catch (SQLException e){
            System.out.println(e.getMessage());
        }
        finally {
            sqlHelperTest.colse(con,st,ps,rs);
        }
        obj=MyUtils.ListToJson(li);
        return ReturnModel.getModel(msg,status,obj);
    }

}
